package cn.iocoder.yudao.module.trade.controller.admin.brokerage;

import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;
import cn.iocoder.yudao.module.member.api.user.MemberUserApi;
import cn.iocoder.yudao.module.member.api.user.dto.MemberUserRespDTO;
import cn.iocoder.yudao.module.system.api.user.AdminUserApi;
import cn.iocoder.yudao.module.system.api.user.dto.AdminUserRespDTO;
import cn.iocoder.yudao.module.trade.controller.admin.brokerage.vo.record.BrokerageRecordRespVO;
import cn.iocoder.yudao.module.trade.convert.brokerage.BrokerageRecordConvert;
import cn.iocoder.yudao.module.trade.dal.dataobject.brokerage.BrokerageLogDO;
import cn.iocoder.yudao.module.trade.dal.dataobject.brokerage.BrokerageRecordDO;
import cn.iocoder.yudao.module.trade.service.brokerage.BrokerageLogService;
import cn.iocoder.yudao.module.trade.service.brokerage.BrokerageRecordService;
import cn.iocoder.yudao.module.wine.api.MemberOpenLogApi;
import cn.iocoder.yudao.module.wine.api.dto.MemberOpenLogRespDTO;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;

import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertSet;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;
import cn.iocoder.yudao.module.trade.controller.admin.brokerage.vo.log.BrokerageLogSaveReqVO;
import cn.iocoder.yudao.module.trade.controller.admin.brokerage.vo.log.BrokerageLogRespVO;
import cn.iocoder.yudao.module.trade.controller.admin.brokerage.vo.log.BrokerageLogPageReqVO;

@Tag(name = "管理后台 - 佣金操作日志")
@RestController
@RequestMapping("/trade/brokerage-log")
@Validated
public class BrokerageLogController {

    @Resource
    private BrokerageLogService brokerageLogService;
    @Resource
    private BrokerageRecordService brokerageRecordService;

    @Resource
    private MemberUserApi memberUserApi;
    @Resource
    private MemberOpenLogApi memberOpenLogApi;

    @Resource
    private AdminUserApi adminUserApi;

    @PostMapping("/create")
    @Operation(summary = "创建佣金操作日志")
    @PreAuthorize("@ss.hasPermission('trade:brokerage-log:create')")
    public CommonResult<Long> createBrokerageLog(@Valid @RequestBody BrokerageLogSaveReqVO createReqVO) {
        return success(brokerageLogService.createBrokerageLog(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新佣金操作日志")
    @PreAuthorize("@ss.hasPermission('trade:brokerage-log:update')")
    public CommonResult<Boolean> updateBrokerageLog(@Valid @RequestBody BrokerageLogSaveReqVO updateReqVO) {
        brokerageLogService.updateBrokerageLog(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除佣金操作日志")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('trade:brokerage-log:delete')")
    public CommonResult<Boolean> deleteBrokerageLog(@RequestParam("id") Long id) {
        brokerageLogService.deleteBrokerageLog(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得佣金操作日志")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('trade:brokerage-log:query')")
    public CommonResult<BrokerageLogRespVO> getBrokerageLog(@RequestParam("id") Long id) {
        BrokerageLogDO brokerageLog = brokerageLogService.getBrokerageLog(id);
        return success(BeanUtils.toBean(brokerageLog, BrokerageLogRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得佣金操作日志分页")
    @PreAuthorize("@ss.hasPermission('trade:brokerage-log:query')")
    public CommonResult<PageResult<BrokerageLogRespVO>> getBrokerageLogPage(@Valid BrokerageLogPageReqVO pageReqVO) {
        PageResult<BrokerageLogDO> pageResult = brokerageLogService.getBrokerageLogPage(pageReqVO);

        return success(BeanUtils.toBean(pageResult, BrokerageLogRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出佣金操作日志 Excel")
    @PreAuthorize("@ss.hasPermission('trade:brokerage-log:export')")
    @OperateLog(type = EXPORT)
    public void exportBrokerageLogExcel(@Valid BrokerageLogPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<BrokerageLogDO> list = brokerageLogService.getBrokerageLogPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "佣金操作日志.xls", "数据", BrokerageLogRespVO.class,
                        BeanUtils.toBean(list, BrokerageLogRespVO.class));
    }

    @GetMapping("/get-list")
    @Operation(summary = "根据佣金操作日志Id获取操作记录列表")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('trade:brokerage-log:query')")
    public CommonResult<List<BrokerageRecordRespVO>> getBrokerageLogRecordList(@RequestParam("id") Long id) {
        BrokerageLogDO brokerageLog = brokerageLogService.getBrokerageLog(id);
        if(brokerageLog == null) {
            return success(null);
        }
        List<Integer> ids = brokerageLog.getRecords();
        List<BrokerageRecordDO> list = brokerageRecordService.getBrokerageRecordListByIds(ids);

        // 查询用户信息
        Set<Long> userIds = convertSet(list, BrokerageRecordDO::getUserId);
        userIds.addAll(convertList(list, BrokerageRecordDO::getSourceUserId));
        Map<Long, MemberUserRespDTO> userMap = memberUserApi.getUserMap(userIds);
        //查询缴费信息
        Set<String> tradeNoIds = convertSet(list, BrokerageRecordDO::getBizId);
        Map<String, MemberOpenLogRespDTO> logMap = memberOpenLogApi.getMemberOpenLogMap(tradeNoIds);
        // 拼接数据
        List<BrokerageRecordRespVO> resultList = BrokerageRecordConvert.INSTANCE.convertPage(list, userMap, logMap);

        return success(resultList);
    }
}